<?php

require_once BASEPATH . 'models/authuser.php';

/**
 * Description of WebController
 *
 * @author user
 */
class WebController extends Controller {

    public $auth_user;
    public $view_user;

    public function __construct() {
        parent::__construct();
    }

    public function _before() {
        parent::_before();

        // Determine Scripts
        $base = URL::base_url() . "public/js/";
        $script = array(
            $base . "util.js",
            $base . "calendar.js",
            $base . "validator.js",
            $base . "common.js",
            $base . "ajaxwrap.js",
            $base . "json2.js",
            $base . "cookie.js",
            $base . "chatclient.js",
            $base . "auth_view.js",
            $base . "newsfeed/index.js",
            $base . "photo/thumbnail.js",
            $base . "photo/view.js",
            $base . "wall/wall.js",
            $base . "message/message.js",
        );

        // Get Auth User
        $this->auth_user = new Authuser();
        $this->_set_view_user();

        // Define main link
        $main_link = array(
            'profile' => TRUE,
            'register' => TRUE,
            'login' => TRUE,
            'logout' => TRUE,
        );

        // Determine link visibility
        if ($this->auth_user->is_login()) {
            $main_link['register'] = $main_link['login'] = FALSE;
        } else {
            $main_link['profile'] = $main_link['logout'] = FALSE;
        }

        // Set to View
        $this->view->main_link = $main_link;
        $this->view->title = "Picdrops";
        $this->view->scripts = $script;
    }

    private function _set_view_user() {
        if (isset($_GET['uid'])) {
            if (!$this->auth_user->is_login()) {
                URL::redirect('home', FALSE);
            }


//            if ($_GET['uid'] == $this->auth_user->user->id) {
//                URL::redirect('profile', FALSE);
//            }

            $user = new ORM('user');
            $user->find($_GET['uid']);
            if ($user->id == null) {
                URL::redirect('profile', FALSE);
            } else {
                $this->view_user = $user;
            }
        } else {
            $this->view_user = $this->auth_user->user;
        }
    }

    public function index() {
        $controller = $this->router->controller;
        $this->view->content = View::factory("$controller/index");
    }

}

?>